﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO.Ports;
using RS232;

namespace UI.Online.SubForms
{
    public partial class frmRS232Settings : Form
    {
        public frmOnlineUI2 fM = null;
        RS232Reader TestReader = new RS232Reader();
        string PortName;
        int BaudRate;
        int StringFormatSelection;

        string[] baudRates = new string[] { "75", "110", "300", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
        public frmRS232Settings()
        {
            InitializeComponent();
        }

        public frmRS232Settings(Form callingForm, string portName, int baudRate, int stringFormatSelection)
            : this()
        {
            fM = callingForm as frmOnlineUI2;
            PortName = portName;
            BaudRate = baudRate;
            StringFormatSelection = stringFormatSelection;
        }

        private void frmRS232Settings_Load(object sender, EventArgs e)
        {
            foreach (string name in SerialPort.GetPortNames())
            {
                cbbComPort.Items.Add(name);
            }

            foreach (string str in baudRates)
            {
                cbbBaudRate.Items.Add(str);
            }

            cbbComPort.Text = PortName;
            cbbBaudRate.Text = BaudRate.ToString();
            //txtStringName.Text = StringName;
            switch (StringFormatSelection)
            {
                case 0:
                    rbtStringFormat1.Checked = true;
                    break;
                case 1:
                    rbtStringFormat2.Checked = true;
                    break;
                default:
                    rbtStringFormat1.Checked = false;
                    rbtStringFormat2.Checked = false;
                    break;
            }
        }

        private bool SetRs232Info(ref RS232Reader reader)
        {
            try
            {
                reader.Port.PortName = cbbComPort.Text;
                reader.Port.BaudRate = int.Parse(cbbBaudRate.Text);
                //reader.StringName = txtStringName.Text;
                if (rbtStringFormat2.Checked)
                {
                    reader.StringFormat = "StartString,KP,Easting,Northing,";
                }
                else
                {
                    reader.StringFormat = "StartString,KP,Easting,Northing,CP,";
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            if (this.fM.bIsRecording)
            {
                this.fM.SurveyDataReader.StopLogging();
            }
            if (this.fM.SurveyDataReader.IsOpen)
            {
                this.fM.SurveyDataReader.Close();
            }

            if (SetRs232Info(ref TestReader))
            {
                if (!TestReader.Port.IsOpen)
                {
                    TestReader.RS232DataReceived += new EventHandler<RS232DataReceivedEventArgs>(TestReader_RS232DataReceived);
                    TestReader.Open();
                }
            }
        }

        private void TestReader_RS232DataReceived(object sender, RS232DataReceivedEventArgs e)
        {
            rtfData.Text =
                "String: " + e.AllFieldsString +
                "\nKP: " + e.Data.KP +
                "\nEasting: " + e.Data.Easting +
                "\nNorthing: " + e.Data.Northing +
                "\nCP: " + e.Data.CP;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (TestReader.Port.IsOpen)
            {
                TestReader.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.fM.bIsRecording)
            {
                this.fM.SurveyDataReader.StopLogging();
            }
            if (this.fM.SurveyDataReader.IsOpen)
            {
                this.fM.SurveyDataReader.Close();
            }
            if (TestReader.IsOpen)
            {
                TestReader.Close();
            }
            if (SetRs232Info(ref (this.fM.SurveyDataReader)))
            {
                if (!this.fM.SurveyDataReader.Port.IsOpen)
                {
                    this.fM.SurveyDataReader.Open();
                }
            }
            this.Close();
        }
    }
}
